<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span>配置名称:</span>
        <el-input v-model="listQuery.name" clearable placeholder="请输入配置名称" />
      </div>
      <div class="filter-item">
        <span>配置编码:</span>
        <el-input v-model="listQuery.code" clearable placeholder="请输入配置编码" />
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>

    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['sys:config:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :data="list" :row-class-name="tableRowClass" highlight-current-row border header-cell-class-name="header-cell">
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column min-width="150" label="配置名称">
        <template #default="{row}">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column min-width="150" label="配置编码">
        <template #default="{row}">
          {{ row.code }}
        </template>
      </el-table-column>
      <el-table-column min-width="150" label="参数值">
        <template #default="{row}">
          <image-gallery v-if="row.type === '2'" v-model="row.value" />
          <file-download-list v-else-if="row.type === '3'" v-model="row.value" />
          <span v-else-if="row.type === '5'">{{ dictLabel(row.value, row.setting) }}</span>
          <span v-else>{{ row.value }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="150" label="备注">
        <template #default="{row}">
          {{ row.remarks }}
        </template>
      </el-table-column>
      <el-table-column min-width="150" label="操作">
        <template #default="{row}">
          <el-button v-permission="['sys:config:update']" size="small" type="primary" plain @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="!row.isSys" v-permission="['sys:config:delete']" size="small" plain type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <config-form ref="form" @refresh-list="getList" />
  </div>
</template>

<script>
import { deleteConfig, fetchConfigList } from '@/api/sys/config/config'
import configForm from './configForm'
import Pagination from '@/components/Pagination/index.vue'
import ImageGallery from '@/components/ImageGallery/imageGallery.vue'
import FileDownloadList from '@/components/FileList/fileDownloadList.vue'

export default {
  name: 'ConfigList',
  components: { FileDownloadList, ImageGallery, Pagination, configForm },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        name: undefined,
        code: undefined,
        page: 1,
        limit: this.$store.dictStore.defaultPageSize
      },
      pageArray: this.$store.dictStore.pageArray
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 刷新列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchConfigList(this.listQuery).then(response => {
        this.listLoading = false
        if (response.data.success) {
          this.total = response.data.total
          this.list = response.data.data
        }
      })
    },
    /**
     * 新增
     */
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 编辑
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    /**
     * 删除
     * @param row 选中数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteConfig([row.id]).then(response => {
          if (response.data.success) {
            row.$delAnim = true
            this.getList(false)
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    }
  }
}
</script>

